package cz.fabian.android.placeLocator;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AddActivity extends Activity {
	private LocationManager locationManager;
	private LocationListener locationListener;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        
        // Acquire a reference to the system Location Manager
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        locationListener = new LocationListener() {
        	
            public void onLocationChanged(Location location) {
            	TextView lat = (TextView) findViewById(R.id.lat);
            	lat.setText(String.valueOf(location.getLatitude()));
		        TextView lon = (TextView) findViewById(R.id.lon);
		        lon.setText(String.valueOf(location.getLongitude()));
		        TextView accuracy = (TextView) findViewById(R.id.accuracy);
		        accuracy.setText(String.valueOf(location.getAccuracy()) + "m");
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {
            	if (provider.equals(LocationManager.GPS_PROVIDER)) {
            		locationManager.removeUpdates(locationListener);
    				locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            	}
            }
        };
        
        // try to acquire location from GPS
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        
        Button button = (Button) findViewById(R.id.save);
        button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DbAdapter helper = new DbAdapter(getBaseContext());
		        helper.open();
		        
		        TextView title = (TextView) findViewById(R.id.title);
		        TextView lat = (TextView) findViewById(R.id.lat);
		        TextView lon = (TextView) findViewById(R.id.lon);
		        
		        helper.create(
	        		String.valueOf(title.getText()),
	        		Float.valueOf(lat.getText().toString()), //FIXME: decimal
	        		Float.valueOf(lon.getText().toString()) //FIXME: decimal
        		);
		        
		        Intent mainActivity = new Intent(getBaseContext(), MainActivity.class);
	    		startActivity(mainActivity);
			}
		});
	}
	
	@Override
	public void onPause() {
		super.onPause();
		locationManager.removeUpdates(locationListener);
	}
}
